import shutil
from config.env import UploadConfig
import time
import os


def job(*args, **kwargs):

    today = time.strftime("%Y_%m_%d", time.localtime(time.time()))
    yesterday = time.strftime("%Y_%m_%d", time.localtime(time.time() - 1 * 86400))

    upload_dir = f"{UploadConfig.UPLOAD_PATH}/ocr"
    download_dir = f"{UploadConfig.DOWNLOAD_PATH}/ocr"

    # 获取目录下的所有目录
    upload_subdirs = [
        d for d in os.listdir(upload_dir) if os.path.isdir(os.path.join(upload_dir, d))
    ]
    download_subdirs = [
        d
        for d in os.listdir(download_dir)
        if os.path.isdir(os.path.join(download_dir, d))
    ]

    for subdir in upload_subdirs:
        if subdir.startswith(today) or subdir.startswith(yesterday):
            continue
        else:
            shutil.rmtree(os.path.join(upload_dir, subdir))

    for subdir in download_subdirs:
        if subdir.startswith(today) or subdir.startswith(yesterday):
            continue
        else:
            shutil.rmtree(os.path.join(download_dir, subdir))
